package engine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import engine.ARFFReader.ProjectPropertyDescriptor;

public abstract class Ranges {

	public static HashMap<String, Double> calculateUsing(ArrayList<ProjectPropertyDescriptor> characteristics, ArrayList<Project> oldProjects, Project newProject) {
		HashMap<String, Double> ranges = new HashMap<String, Double>();
		ArrayList<Project> oldProjectCopy = (ArrayList<Project>)oldProjects.clone();
		
		for (ProjectPropertyDescriptor pd : characteristics) {
			
			final String name = pd.getName();
			
			Collections.sort(oldProjectCopy, new Comparator<Project>() {

				public int compare(Project o1, Project o2) {
					double diff = o1.getCharacteristic(name).getValue() - o2.getCharacteristic(name).getValue();
					if (diff > 0)
						return 1;
					else if (diff < 0)
						return -1;
					else
						return 0;
				}
			});
			ranges.put(name, Math.abs(oldProjectCopy.get(oldProjectCopy.size()-1).getCharacteristic(name).getValue()-
									oldProjectCopy.get(0).getCharacteristic(name).getValue()));
			
		}
		{
			final String name = "LOC";
			Collections.sort(oldProjectCopy, new Comparator<Project>() {

				public int compare(Project o1, Project o2) {
					double diff = o1.getCharacteristic("LOC").getValue() - o2.getCharacteristic("LOC").getValue();
					if (diff > 0)
						return 1;
					else if (diff < 0)
						return -1;
					else
						return 0;
				}
			});
			
			//TODO använd 0->5 istället för getValue();
			ranges.put(name, Math.abs(oldProjectCopy.get(oldProjectCopy.size()-1).getCharacteristic(name).getValue()-
					oldProjectCopy.get(0).getCharacteristic(name).getValue()));
			
		}
		return ranges;
	}
}
